package QASystem;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class CustomMatcher {
	public static final String patternString = "[0-9]+_[0-9]*_[a-z]+";
	public static final String patternFileName = "Configuration/pattern.xml";

	public static Logger logger = Logger.getLogger(CustomMatcher.class
			.getSimpleName());

	/**
	 * 
	 * @param sentence
	 *            give sentence for finding object(s).
	 * @param patternStr
	 *            a input pattern for matching object(s) in sentence
	 * @return {@link List<String>} a List of String(s) of words in sentence
	 *         that matchs give pattern.
	 */
	public static List<String> findMatchingPatternValues(String sentence,
			String patternStr) {
		if (patternStr.equals("")) {
			patternStr = patternString;
		}
		Pattern pattern = Pattern.compile(patternStr);
		Matcher matcher = pattern.matcher(sentence);
		List<String> matchStrings = new ArrayList<String>();
		while (matcher.find()) {
			matchStrings.add(matcher.group());
		}
		return matchStrings;
	}
	/**
	 * Give us a list of String from sentence that match with given pattern in {@link pattern.xml}
	 * @param sentence  sentence need to be analysed
	 * @return
	 * 		a List of String that are matched in sentence with given pattern.
	 */
	public static List<String> findMatchingPatternValues(String sentence) {
		String patternStr = "";
		try {
			patternStr = readPatternFromResource(patternFileName);
		} catch (ParserConfigurationException e) {
			logger.log(Level.SEVERE, e.getMessage());
		} catch (SAXException e) {
			logger.log(Level.SEVERE, e.getMessage());
		} catch (IOException e) {
			logger.log(Level.SEVERE, e.getMessage());
		}

		if (patternStr.equals("")) {
			patternStr = patternString;
		}
		Pattern pattern = Pattern.compile(patternStr);
		Matcher matcher = pattern.matcher(sentence);
		List<String> matchStrings = new ArrayList<String>();
		while (matcher.find()) {
			matchStrings.add(matcher.group());
		}
		return matchStrings;
	}

	public static String readPatternFromResource(String fileName)
			throws ParserConfigurationException, SAXException, IOException {
		String pattern = "";
		File file = new File(fileName);
		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
				.newInstance();
		DocumentBuilder documentBuilder = documentBuilderFactory
				.newDocumentBuilder();
		Document xmlDocument = documentBuilder.parse(file);
		xmlDocument.getDocumentElement().normalize();
		NodeList nodeList = xmlDocument.getElementsByTagName("pattern");		
		Node patternNode = nodeList.item(0);
		if (patternNode.getNodeType() == Node.ELEMENT_NODE){
			pattern = patternNode.getChildNodes().item(0).getNodeValue();					
		}		
		return pattern;
	}
	
	public static String readSentence(String fileName)
			throws ParserConfigurationException, SAXException, IOException {
		String pattern = "";
		File file = new File(fileName);
		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
				.newInstance();
		DocumentBuilder documentBuilder = documentBuilderFactory
				.newDocumentBuilder();
		Document xmlDocument = documentBuilder.parse(file);
		xmlDocument.getDocumentElement().normalize();
		NodeList nodeList = xmlDocument.getElementsByTagName("sentence");		
		Node patternNode = nodeList.item(0);
		if (patternNode.getNodeType() == Node.ELEMENT_NODE){
			pattern = patternNode.getChildNodes().item(0).getNodeValue();					
		}		
		return pattern;
	}
	

}
